﻿// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

// add the following namespaces
using MyTravelPostModel;
using System.Data.Objects;
using System.Collections.Generic;

namespace ObjectServices_ASPNET_CS
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // check the post back
            if (IsPostBack)
                return;

            // create a short lived context and send back the travel blogs.
            using (MyTravelEntities travelEntities = new MyTravelEntities())
            {
                // only get the blogs that are travel blogs
                var travelBlogs = from tb in travelEntities.Blogs
                                  where tb.BlogPosts.Count > 0
                                  select tb;

                // bind to the grid
                TravelBlogsGrid.DataSource = travelBlogs;
                TravelBlogsGrid.DataBind();

                // get the name of the people, and store the list of people in view state.
                dropDownPeople.DataTextField = "Name";
                List<Person> people = travelEntities.People.ToList();
                ViewState["People"] = people;

                dropDownPeople.DataSource = people;
                dropDownPeople.DataBind();
            }
        }

        protected void buttonCreateNewBlog_Click(object sender, EventArgs e)
        {
            // get the people out and get the id to past to the new blog post page.
            List<Person> people = (List<Person>)ViewState["People"];
            int peopleID = people[dropDownPeople.SelectedIndex].PersonID;

            // create a response and redirect passing the person ID.
            Response.Redirect("NewBlogPost.aspx?PersonID=" + peopleID.ToString());
        }
    }
}
